package square;

import java.util.Arrays;

/*
小方格的父类
    属性： square数组用于创建四个小方格
    方法:左移一格，右移一格，下落一格，变形
*/
public class ParentSquare  {
    //数组长度为4，每个形状都是有四个小方格组成的
    protected Cell[] cells = new Cell[4];
    //编写旋转状态
    protected State[] states;
    //声明旋转次数
    protected int count = 10000;
    //四方格旋转状态的内部类
    class State{
        //表示四方格的相对位置
        public int row0,col0,row1,col1,row2,col2,row3,col3;

        public State() {
        }

        public State(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3) {
            this.row0 = row0;
            this.col0 = col0;
            this.row1 = row1;
            this.col1 = col1;
            this.row2 = row2;
            this.col2 = col2;
            this.row3 = row3;
            this.col3 = col3;
        }

        @Override
        public String toString() {
            return "State{" +
                    "row=" + row0 +
                    ", col0=" + col0 +
                    ", row1=" + row1 +
                    ", col1=" + col1 +
                    ", row2=" + row2 +
                    ", col2=" + col2 +
                    ", row3=" + row3 +
                    ", col3=" + col3 +
                    '}';
        }

        public int getRow() {
            return row0;
        }

        public void setRow(int row) {
            this.row0 = row;
        }

        public int getCol0() {
            return col0;
        }

        public void setCol0(int col0) {
            this.col0 = col0;
        }

        public int getRow1() {
            return row1;
        }

        public void setRow1(int row1) {
            this.row1 = row1;
        }

        public int getCol1() {
            return col1;
        }

        public void setCol1(int col1) {
            this.col1 = col1;
        }

        public int getRow2() {
            return row2;
        }

        public void setRow2(int row2) {
            this.row2 = row2;
        }

        public int getCol2() {
            return col2;
        }

        public void setCol2(int col2) {
            this.col2 = col2;
        }

        public int getRow3() {
            return row3;
        }

        public void setRow3(int row3) {
            this.row3 = row3;
        }

        public int getCol3() {
            return col3;
        }

        public void setCol3(int col3) {
            this.col3 = col3;
        }
    }
    //实现顺时针旋转四方格方法
    public void rotateRight(){
        //如果长度等于0 说明该四方格没有额外的旋转状态，直接返回，不执行任何旋转操作
        if(states.length == 0) return ;
        //旋转次数加1
        count++;
        //得到当前应该使用的旋转状态
        State s= states[count % states.length];
        //选择 cells[0] 作为基准单元格
        Cell cell = cells[0];
        //获取基准单元格的行和列位置。
        int row = cell.getRow();
        int col = cell.getCol();
        //根据 s 中存储的相对位置信息和基准单元格的位置，更新 cells[1] 到 cells[3] 的位置
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }
    //实现逆时针旋转四方格方法
    public void rotateLeft(){
        if(states.length == 0) return ;
        //旋转次数减1
        count--;
        State s= states[count % states.length];
        Cell cell = cells[0];
        int row = cell.getRow();
        int col = cell.getCol();
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }
    //直接调用Square类中的left，right，drop方法
    // 来实现小方格的移动，下落，
    public void moveleft(){
        for (Cell cell : cells) cell.left();
    }
    public void moveRight(){
        for (Cell cell : cells) cell.right();
    }
    public void moveDrop(){
        for (Cell cell : cells) cell.drop();
    }
    //随机生成四方格的方法
    public static ParentSquare randomOne(){
        //一共七个图形
        int num = (int)(Math.random()*7);
        ParentSquare parentSquare = null;
        switch(num){
            case 0:
                parentSquare = new I();
                break;
            case 1:
                parentSquare = new J();
                break;
            case 2 :
                parentSquare = new L();
                break;
            case 3 :
                parentSquare = new O();
                break;
            case 4 :
                parentSquare = new S();
                break;
            case 5 :
                parentSquare = new T();
                break;
            case 6 :
                parentSquare = new Z();
                break;

        }
        return parentSquare;
    }

    @Override
    public String toString() {
        return "ParentSquare{" +
                "squares=" + Arrays.toString(cells) +
                ", states=" + Arrays.toString(states) +
                ", count=" + count +
                '}';
    }
    }
